﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.Navigation;
using CdcSoftware.Pivotal.Engine.Types.Searches;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_AddOrderProductItem : FormClientTask
    {
        #region Class Variables
        #endregion

        /// <summary>
        /// This method gets called when the form meta data and data have both been loaded,
        /// and the form UI has finished drawing. Override this method to perform custom
        /// form initialization.
        /// </summary>
        /// <history>
        /// #Revision   Date        Author  Description
        /// 6.0.12.0    31/01/2013  DS      Initial Version
        /// </history>
        public override void OnFormInitialized()
        {
            try
            {
                base.OnFormInitialized();
                if (this.TransitionPointParameter.UserDefinedParametersNumber == 1 && TypeConvert.ToBoolean(this.TransitionPointParameter.GetUserDefinedParameter(1)))
                    this.FormControl.GetControlByName("PivotalGroupBox1").WindowsControl.Text = "Deposit/Reservation";
                else if (this.TransitionPointParameter.UserDefinedParametersNumber == 1 && this.TransitionPointParameter.GetUserDefinedParameter(1) != null
                    && !TypeConvert.ToBoolean(this.TransitionPointParameter.GetUserDefinedParameter(1)))
                {
                    this.FormControl.GetControlByName("PivotalGroupBox1").WindowsControl.Text = "Trade-In";
                    ((PivotalForeignKey)this.FormControl.GetControlByDataName("Product_Id")).ForeignKeyFilterId =
                        this.SystemClient.UserProfile.GetMetaItem<SystemSearch>("RE_Products with Type as Trade-In").Id.ToByteArray();
                    ((PivotalForeignKey)this.FormControl.GetControlByDataName("Product_Id")).ReadOnly = false;
                }
                else
                    this.FormControl.GetControlByName("PivotalGroupBox1").WindowsControl.Text = "Fees for IPP";
               
                ((PivotalForeignKey)this.FormControl.GetControlByDataName("Product_Id")).Required = true;
                Globals.RemoveButtonBarButton(new string[] { "Apply", "Refresh", "Delete" });
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }

        /// <summary>
        /// This function updates the Form
        /// </summary>
        /// <history>
        /// #Revision   Date        Author  Description
        /// 6.0.12.0    28/02/2013  DS      Initial Version
        /// </history>
        public override bool SaveRecord()
        {
            bool saveRecord = false;

            try
            {
                //DS: 20052013 - Unit Price always should be Positive only.
                //if (this.TransitionPointParameter.UserDefinedParametersNumber == 1 && this.TransitionPointParameter.GetUserDefinedParameter(1) != null
                //    && !TypeConvert.ToBoolean(this.TransitionPointParameter.GetUserDefinedParameter(1)))
                this.PrimaryDataRow["Unit_Price"] = Math.Abs(TypeConvert.ToDouble(this.PrimaryDataRow["Unit_Price"]));
                saveRecord = base.SaveRecord();
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
            return saveRecord;
        }
    }
}
